package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataTaskPo;
import com.csii.adhoc.dataready.mapper.TDataTaskMapper;
import com.csii.adhoc.dataready.service.TDataTaskService;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataTaskService")
@Slf4j
public class TDataTaskServiceImpl implements TDataTaskService {

    @Autowired
    TDataTaskMapper tDataTaskMapper;

    @Override
    @Transactional
    public void tDataTaskMaintain(TDataTaskPo tDataTaskPo, String optMode) {
        //检查上传的值

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                tDataTaskPo.setCreatedBy(tDataTaskPo.getUpdatedBy());
                insertOneTDataTask(tDataTaskPo);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                updateTDataTaskByPrimaryKey(tDataTaskPo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteTDataTaskByPrimaryKey(tDataTaskPo.getModelId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void insertOneTDataTask(TDataTaskPo tDataTaskPo) {
        if (StringUtils.isEmpty(tDataTaskPo.getModelId())) {
            tDataTaskPo.setModelId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataTaskPo.setCreatedDt(tDataTaskPo.getUpdatedDt());
        tDataTaskMapper.insertOneTDataTask(tDataTaskPo);
    }

    @Transactional
    @Override
    public int updateTDataTaskByPrimaryKey(TDataTaskPo tDataTaskPo) {
        return tDataTaskMapper.updateByPrimaryKeySelective(tDataTaskPo);
    }

    @Transactional
    @Override
    public int deleteTDataTaskByPrimaryKey(String ModelId) {
        return tDataTaskMapper.deleteByPrimaryKey(ModelId);
    }

    @Override
    public TDataTaskPo selectByPrimaryKey(String ModelId) {
        return tDataTaskMapper.selectByPrimaryKey(ModelId);
    }

    @Override
    public List<TDataTaskPo> selectListTDataTask(String type) {
        return tDataTaskMapper.selectListTDataTask(type);
    }

    @Override
    public Map selCustMapDataDbTable(String sql){
        return tDataTaskMapper.selCustMapDataDbTable(sql);
    }

    public List selCustListDataDbTable(String sql){
        return tDataTaskMapper.selCustListDataDbTable(sql);
    }
}
